package com.hb.game.pk.ui.activity.game.red;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.adpter.TotalRedListAdpter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.ReceiveHBBean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.SpannableStringUtils;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TotalRedActivity extends BaseActivity<ReceiveHBBean> {
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.iv_more)
    LinearLayout ivMore;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_tyoe)
    TextView tv_tyoe;
    @BindView(R.id.tv_tishi)
    TextView tvTishi;
    @BindView(R.id.tv_count)
    TextView tvCount;
    MaterialDialog choseDialog;
    List<ReceiveHBBean.DataBean.ListBean> list;
    int type = 1;
    int p = 1;
    private List<ReceiveHBBean.DataBean.ListBean> listMore;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_total_red;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        tvMoney.setText(SpannableStringUtils.getBuilder("0").setProportion(1.5f)
                .setForegroundColor(getResources().getColor(R.color.c_000000))
                .append("元").setForegroundColor(getResources().getColor(R.color.c_000000))
                .create());
        tvCount.setText(String.format(getResources().getString(R.string.activity_total_count), "0"));
        ivMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View inflate = View.inflate(TotalRedActivity.this, R.layout.dialog_tatal_type, null);
                TextView tvSD = (TextView) inflate.findViewById(R.id.tv_sd);
                TextView tvFC = (TextView) inflate.findViewById(R.id.tv_fc);
                tvSD.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        type = 1;
                        p = 1;
                        if (choseDialog != null && choseDialog.isShowing())
                            choseDialog.dismiss();
                        onRefreshData();
                    }
                });
                tvFC.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        type = 2;
                        p = 1;
                        if (choseDialog != null && choseDialog.isShowing())
                            choseDialog.dismiss();
                        onRefreshData();
                    }
                });
                if (choseDialog == null) {
                    choseDialog = new MaterialDialog.Builder(TotalRedActivity.this)
                            .customView(inflate, false).show();
                } else {
                    if (choseDialog.isShowing()) {
                        choseDialog.dismiss();
                    } else {
                        choseDialog.show();
                    }
                }
            }
        });
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        requestNet(false);
    }

    @Override
    protected void onLoadMore() {
        super.onLoadMore();
        requestNet(true);
    }

    @Override
    protected void setMoreData() {
        super.setMoreData();
        if (listMore != null)
            mAdapter.addData(listMore);
    }

    private void requestNet(final boolean isMore) {
        if (type == 1) {
            tv_tyoe.setText("收到的红包");
            tvTishi.setText("共收到");
            call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).recivelist(p);
        } else {
            tv_tyoe.setText("发出的红包");
            tvTishi.setText("共发出");
            call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).sendlist(p);
        }
        call.enqueue(new Callback<ReceiveHBBean>() {
            @Override
            public void onResponse(Call<ReceiveHBBean> call, Response<ReceiveHBBean> response) {
                ReceiveHBBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                ReceiveHBBean.DataBean.UserBean user = body.getData().getUser();
                String trim = StringUtils.getString(user.getFace());
                int res = 0;
                if (StringUtils.isEmpty(trim)) {
                    res = R.mipmap.ic_head_normal;
                } else {
                    int i = Integer.parseInt(trim);
                    res = APP.headResource.get(i - 1);
                }
                ivHead.setBackgroundResource(res);
                if (isMore) {
                    listMore = body.getData().getList();
                    loadMoreSuccess();
                    if (listMore != null && listMore.size() > 0) {
                        p++;
                    } else {
                        loadMoreEnd();
                        p = 1;
                    }
                } else {
                    list = body.getData().getList();
                    initAdapter();
                    p++;
                    if (type == 1) {
                        tvCount.setText(String.format(getResources().getString(R.string.activity_total_count_1), body.getData().getCount() + ""));
                    } else {
                        tvCount.setText(String.format(getResources().getString(R.string.activity_total_count), body.getData().getCount() + ""));
                    }
                    if (StringUtils.isEmpty(body.getData().getSum())) {
                        tvMoney.setText(String.format(getResources().getString(R.string.get_total_money), "0"));
                    } else {
                        tvMoney.setText(String.format(getResources().getString(R.string.get_total_money), (Float.parseFloat(body.getData().getSum()) / 100f) + ""));
                    }
                    initAdapter();
                }
            }

            @Override
            public void onFailure(Call<ReceiveHBBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new TotalRedListAdpter(R.layout.item_total_red_list);
            mAdapter.setNewData(list);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    List<ReceiveHBBean.DataBean.ListBean> data = (List<ReceiveHBBean.DataBean.ListBean>) adapter.getData();
                    ReceiveHBBean.DataBean.ListBean listBean = data.get(position);
                    String hb_id = "";
                    if (type == 1) {
                        hb_id = listBean.getHb_id();
                    } else {
                        hb_id = listBean.getId();
                    }
                    Intent intent = new Intent();
                    intent.putExtra("clickHbId", hb_id);
                    startActivity(OpenRedActivity.class, intent);
                }
            });
        } else {
            mAdapter.setNewData(list);
        }
        super.initAdapter();
    }


    @OnClick({R.id.ll_back, R.id.iv_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                this.finish();
                break;
            case R.id.iv_more:
                break;
        }
    }
}
